import Request from './request'
import { REQUEST_BASE_URL,REQUEST_BASE_URL1 } from '../../config/index.config.js'
import { getUploadParamList} from '../../utils/index'
Request.DEFAULT_BASE_URL = REQUEST_BASE_URL
Request.DEFAULT_BASE_URL1 = REQUEST_BASE_URL1
const requestIntercept = {
  // 发出请求时的回调函数
  config (config) {
	const data = config.data ? config.data : {};
	const params = config.params ? config.params : {};
	const { showLoading = true, showDebug = false } = config.data || {}
	if(!(config.data instanceof Array)) {
		delete(data.showLoading);
		delete(data.showDebug);
	}
    showLoading > 0 && uni.showLoading({
      title: '加载中',
      icon: 'loading',
      mask: true
    })

    const token=uni.getStorageSync('Authorization')
	console.log('token')


    config = Object.assign({}, config, {
      url: `${config.url}`,
      header: {
        'content-type': config.contentType ? config.contentType : 'application/x-www-form-urlencoded',
        'Authorization': token
      },
      data,
	  params,
      method: config.method || 'POST'
    })
    showDebug > 0 && console.log(`data:`, config.data);
    console.log(111,config);
    return config
  },
  
  // 请求成功后的回调函数
  async success (response){
    uni.hideLoading()
    const { statusCode, data, config } = response
    const { showLoading = true, showToast = true, showDebug = false } = config.data || {}
    showLoading > 0 && uni.hideLoading()
    showDebug > 0 && console.log(`response:`, response);
    let errorMesg = ''
    // 可以在这里对收到的响应数据对象进行加工处理
    switch (statusCode) {
      case 200:
        let { code, message,extend } = data
        if (code != 1 && code != 63) {
          // 处理token过期、禁用、服务端redis异常问题
          if(code === 9){
            uni.showToast({
              title:  message,
              icon: 'none',
              duration: 5000
            })}
            else if (code === "403"){
              console.log(message,extend)
              let [a,{code, errMsg }] = await uni.login({provider: 'weixin'});
              if (errMsg === "login:ok"){
                const {data} =await getUploadParamList(
                {code:code})
              if(data.code=="00000"){
                // 未注册过显示注册
                uni.setStorageSync('openId', data.extend.openId);
                uni.setStorageSync('sessionkey', data.extend.sessionkey)	
                uni.setStorageSync('Authorization', data.extend.Authorization)
              }	
              } 	
            }
            else if (code=="11111"){
             console.log(message,extend)
            }
           else{
            showToast && setTimeout(async () => {
              await uni.showToast({
                title: message,
                icon: 'none',
                duration: 2000
              })
            }, 600);
          }


        }
        break
      case 500:
      case 502:
      case 503:
      case 504:
        errorMesg = '服务器出错'
        break
    }
    
    if (errorMesg.length > 0) {
      uni.showToast({
        title: errorMesg,
        icon: 'none'
      })
      throw new Error(errorMesg)
    }
    return response
  },
  fail (error) {
    uni.showToast({
      title:  error.message || error.errorMessage || error.errMsg || '网络连接失败',
      icon: 'none',
      duration: 5000
    })
  
  }
}
export default new Request(requestIntercept)
